home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pcmagazi / 1989 / 18 / dump.asm < prev    next >
Assembly Source File  |  1989-09-18  |  8KB  |  257 lines

  1.  
  2.  
  3.         title   DUMP --- display file contents
  4.         page    55,132
  5.         .386
  6.  
  7. ; DUMP.ASM      Display contents of file in hex and ASCII.
  8. ;               80386 version for Phar Lap tools.
  9. ;               Also requires 80386 versions of ARGV.ASM,
  10. ;               ARGC.ASM, and HEXASC.ASM.
  11. ;
  12. ; Copyright (C) 1989 Ziff Communications Co.
  13. ; PC Magazine * Ray Duncan
  14. ;
  15. ; Build:        C>386ASM  DUMP
  16. ;               C>386ASM  ARGC  
  17. ;               C>386ASM  ARGV  
  18. ;               C>386ASM  HEXASC
  19. ;               C>386LINK DUMP ARGC ARGV HEXASC  -EXE DUMP
  20. ; MAKE File:    argc.obj : argc.asm
  21. ;          386asm argc
  22. ;
  23. ;        argv.obj : argv.asm
  24. ;          386asm argv
  25. ;
  26. ;        hexasc.obj : hexasc.asm
  27. ;          386asm hexasc
  28. ;
  29. ;        dump.obj : dump.asm
  30. ;          386asm dump
  31. ;
  32. ;        dump.exe : dump.obj argv.obj argc.obj hexasc.obj
  33. ;          386link dump,argc,argv hexasc -exe dump
  34. ;          bind386 c:\tools\run386b.exe dump.exp
  35. ;
  36. ;
  37. ; Usage:        C>DUMP unit:\path\filename.exe [ >device ]
  38.  
  39. cr      equ     0dh        ; ASCII carriage return
  40. lf      equ     0ah         ; ASCII line feed
  41. tab     equ     09h        ; ASCII tab code
  42. blank   equ     20h        ; ASCII space code
  43.  
  44. blksize equ     16        ; input file record size
  45.  
  46. stdin   equ     0        ; standard input handle
  47. stdout  equ     1        ; standard output handle
  48. stderr  equ     2        ; standard error handle
  49.  
  50.  
  51. _TEXT   segment dword use32 public 'CODE'
  52.  
  53.         assume  cs:_TEXT,ds:_DATA,es:_DATA,ss:STACK
  54.  
  55.         extrn   argc:near    ; get no. of command line args.
  56.         extrn   argv:near    ; get addr. of command line arg.
  57.         extrn   d2asc:near    ; convert dword to hex ASCII
  58.         extrn   b2asc:near    ; convert byte to hex ASCII
  59.  
  60. dump    proc    far        ; entry point from MS-DOS
  61.  
  62.         call    argc        ; count command arguments
  63.         cmp     eax,2        ; are there 2 arguments?
  64.         je      dump1        ; yes, proceed
  65.  
  66.         mov     edx,offset msg1    ; missing or illegal filespec.
  67.         mov     ecx,msg1_len
  68.         jmp     dump7        ; display error message and exit
  69.  
  70. dump1:                    ; copy filename from command
  71.             ; tail to local buffer
  72.  
  73.         mov     eax,1        ; get pointer to command
  74.         call    argv        ; tail argument in ES:EBX
  75.  
  76.         mov     ecx,eax    ; let ECX = length
  77.         mov     edi,offset fname; DS:EDI = local buffer address
  78.  
  79. dump2:  mov     al,es:[ebx]    ; copy filename byte by byte
  80.         mov     [edi],al
  81.         inc     ebx
  82.         inc     edi
  83.         loop    dump2
  84.  
  85.         mov     ax,ds        ; make our data segment
  86.         mov     es,ax        ; addressable by ES too
  87.  
  88.             ; now open the file...
  89.         mov     eax,3d00h    ; Fxn 3DH = open file
  90.             ; mode 0 = read only
  91.         mov     edx,offset fname; DS:EDX = filename
  92.         int     21h        ; transfer to MS-DOS
  93.         mov     fhandle,eax    ; save file handle if any
  94.         jnc     dump3        ; jump, open successful
  95.  
  96.         mov     edx,offset msg2    ; open failed, display 
  97.         mov     ecx,msg2_len    ; error message and exit
  98.         jmp     dump7
  99.  
  100. dump3:            ; read block of file data...
  101.         mov     ebx,fhandle    ; EBX = file handle
  102.         mov     ecx,blksize    ; ECX = record length
  103.         mov     edx,offset fbuff; DS:EDX = buffer
  104.         mov     ah,3fh        ; Fxn 3FH = read
  105.         int     21h        ; transfer to MS-DOS
  106.  
  107.         mov     flen,eax    ; save actual length of data
  108.         or      eax,eax    ; end of file reached?
  109.         jne     dump4        ; no, proceed
  110.  
  111.         cmp     dword ptr fptr,0; was this the first record?
  112.         jne     dump6        ; no, exit normally
  113.  
  114.         mov     edx,offset msg3    ; display "empty file"
  115.         mov     ecx,msg3_len    ; error message and exit
  116.         jmp     dump7
  117.  
  118. dump4:  test    fptr,07fh    ; heading needed?
  119.         jnz     dump5        ; jump, not 128-byte boundary
  120.  
  121.             ; display heading...
  122.         mov     edx,offset hdg    ; ES:EDX = heading address
  123.         mov     ecx,hdg_len    ; ECX = heading length
  124.         mov     ebx,stdout    ; EBX = standard output handle
  125.         mov     ah,40h    ; Fxn 40H = write
  126.         int     21h        ; transfer to MS-DOS
  127.  
  128. dump5:  call    cnvblk    ; convert record to ASCII
  129.         
  130.             ; display formatted output...
  131.         mov     edx,offset fout    ; DX:EDX = output address
  132.         mov     ecx,fout_len    ; ECX = output length
  133.         mov     ebx,stdout    ; EBX = standard output
  134.         mov     ah,40h    ; Fxn 40H = write
  135.         int     21h        ; transfer to MS-DOS
  136.  
  137.         jmp     dump3        ; go get another record
  138.  
  139. dump6:            ; close input file...
  140.         mov     ebx,fhandle    ; EBX = file handle
  141.         mov     ah,3eh    ; Fxn 3EH = close file
  142.         int     21h        ; transfer to MS-DOS
  143.  
  144.         mov     eax,4c00h    ; Fxn 4CH = terminate,
  145.             ; return code = 0
  146.         int     21h        ; transfer to MS-DOS
  147.  
  148. dump7:            ; common error exit point...
  149.             ; DS:EDX = message address
  150.             ; ECX = message length
  151.         mov     ebx,stderr    ; standard error handle
  152.         mov     ah,40h    ; Fxn 40H = write
  153.         int     21h        ; transfer to MS-DOS
  154.  
  155.         mov     eax,4c01h    ; Fxn 4CH = terminate,
  156.             ; return code = 1
  157.         int     21h        ; transfer to MS-DOS
  158.         
  159. dump    endp
  160.  
  161.  
  162. cnvblk  proc    near        ; convert record to ASCII
  163.  
  164.         mov     edi,offset fout    ; clear output format 
  165.         mov     ecx,fout_len-2    ; area to blanks
  166.         mov     al,blank
  167.         rep stosb
  168.  
  169.         mov     edi,offset fout    ; convert file offset
  170.         mov     eax,fptr    ; to ASCII for output
  171.         call    d2asc
  172.  
  173.         xor     ebx,ebx    ; reset buffer pointer
  174.  
  175. cb1:    mov     al,[fbuff+bx]    ; fetch byte from buffer
  176.         lea     edi,[ebx+foutb]    ; point to output area
  177.                         
  178.             ; format ASCII part...
  179.         mov     byte ptr [edi],'.'; store '.' as default
  180.         cmp     al,blank    ; in range 20H - 7EH?
  181.         jb      cb2        ; jump, not alphanumeric.
  182.         cmp     al,7eh    ; in range 20H - 7EH?
  183.         ja      cb2        ; jump, not alphanumeric.
  184.         mov     [edi],al    ; store ASCII character.
  185.  
  186. cb2:            ; format hex part...
  187.         mov     edi,ebx    ; calculate output address
  188.         imul    edi,edi,3    ; (position*3) + base address   
  189.         add     edi,offset fouta        
  190.         call    b2asc        ; convert byte to hex 
  191.  
  192.         inc     ebx        ; advance through record
  193.         cmp     ebx,flen    ; entire record converted?
  194.         jne     cb1        ; no, get another byte
  195.  
  196.         add     dword ptr fptr,blksize; update file pointer
  197.  
  198.         ret        ; back to caller
  199.  
  200. cnvblk  endp
  201.  
  202. _TEXT   ends
  203.  
  204.  
  205. _DATA   segment dword use32 public 'DATA'
  206.  
  207. fname   db      64 dup (0)    ; buffer for input filespec
  208.  
  209. fhandle dd      0        ; token from PCDOS for input file.
  210.  
  211. flen    dd      0        ; actual length read
  212.  
  213. fptr    dd      0        ; relative address in file 
  214.  
  215. fbuff   db      blksize dup (?)    ; data from input file
  216.  
  217. fout    db      'nnnnnnnn'    ; formatted output area
  218.         db      blank,blank
  219. fouta   db      16 dup ('nn',blank)
  220.         db      blank
  221. foutb   db      16 dup (blank),cr,lf
  222. fout_len equ    $-fout
  223.  
  224. hdg     db      cr,lf        ; heading for each 128 bytes
  225.         db      11 dup (blank)    ; of formatted output
  226.         db      '0  1  2  3  4  5  6  7  '
  227.         db      '8  9  A  B  C  D  E  F',cr,lf
  228. hdg_len equ     $-hdg
  229.  
  230. msg1    db      cr,lf
  231.         db      'dump: missing file name'
  232.         db      cr,lf
  233. msg1_len equ    $-msg1
  234.  
  235. msg2    db      cr,lf
  236.         db      'dump: file not found'
  237.         db      cr,lf
  238. msg2_len equ    $-msg2
  239.  
  240. msg3    db      cr,lf
  241.         db      'dump: empty file'
  242.         db      cr,lf
  243. msg3_len equ    $-msg3
  244.  
  245. _DATA   ends    
  246.  
  247.  
  248. STACK   segment dword use32 stack 'STACK'
  249.  
  250.         dd      64 dup (?)
  251.  
  252. STACK   ends
  253.  
  254.         end     dump
  255.  
  256.